gravity与layout_gravity的区别

gravity与layout_gravity的区别gravity与layout_gravity的区别

大家好,又见面了,我是你们的朋友全栈君。

含义

gravity是相对于元素本身而言的,用于控制布局中控件的对齐方式,如果没有子控件,则表示内容的对齐方式。gravity常用值有top、bottom、left、right、center等,可以使用”|”将多个值连接起来。

layout_gravity是相对于父布局容器而言的,用于设置在父容器中的对齐方式。

例子

在LinearLayout中放置一个TextView,通过配置gravity和layout_gravity观察TextView出现的位置。
xml文件代码:在TextView中添加属性,android:layout_gravity=”center”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center"//设置TextView在父控件LinearLayout中居中
        android:text="@string/hello_world" />

</LinearLayout>
在LinearLayout中修改代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center">//设置子控件TextView居中
    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center"
        android:text="@string/hello_world" />

</LinearLayout>

gravity与layout_gravity的区别=
gravity与layout_gravity的区别


从上面的运行结果就可以看出,android:layout_gravity=”center”是让TextView显示在Layout的中间。
android:gravity=”center”则让LinearLayout中的子控件TextView居中。
再看一个例子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center_horizontal"//添加代码的位置
        android:layout_gravity="center
        android:text="@string/hello_world" />

</LinearLayout>

gravity与layout_gravity的区别


TextView中没有子控件,那么它会对TextView中的内容起作用。字体在TextView中居中显示。


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159825.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • eplan激活码【2021最新】

    (eplan激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月22日
  • python支持向量机回归_支持向量机——核函数与支持向量回归(附Python代码)[通俗易懂]

    python支持向量机回归_支持向量机——核函数与支持向量回归(附Python代码)[通俗易懂]上期跟大家介绍了支持向量机的一般原理,今天继续跟大家聊聊支持向量机——核函数与支持项链回归。1核函数数据通过某种变换,使原本二维的问题通过某种函数转换到高维的特征空间,而这个函数就称为核函数。核函数有很多种,有线性核函数,多项式核函数,高斯核函数等,其中高斯核函数最为著名。核函数可以说是支持向量机的灵魂,因为现实生活中,我们不大可能通过一个线性的等式就可以完美的解决一个分类问题,总是要经过核函数…

    2022年6月5日
  • Kali 安装 Nessus 详细过程

    Kali 安装 Nessus 详细过程详细介绍在Kali中安装Nessus的详细过程步骤以及插件下载失败解决方法

    2022年10月19日
  • 云打码注册以及使用「建议收藏」

    云打码注册以及使用「建议收藏」这里用云打码演示:http://www.yundama.com/about.html需要注册两个账号,1.普通用户(充值金额),2.开发者用户(创建软件使用)题分和价格表链接:http://www

    2022年8月2日
  • linux redis密码修改,Linux redis 安装并且修改密码

    linux redis密码修改,Linux redis 安装并且修改密码linuxredis安装1、检查是否有redisyum源yuminstallredis2、下载fedora的epel仓库yuminstallepel-release3、安装redis数据库yuminstallredis4、安装完毕后,使用下面的命令启动redis服务启动redisserviceredisstart停止redisserviceredisstop查…

    2022年10月29日
  • 群晖|半洗白后moments正常显示人像、主题、预览「建议收藏」

    群晖|半洗白后moments正常显示人像、主题、预览「建议收藏」Moments不工作群晖重装6.2.3之后,发现moments不能正常工作了,不管是人物还是主题都是空白,还会显示有XXX张照片待发现,但会停在这儿。这如何能忍,网络上找到替换文件大法,没啥用,这台虚拟机内黑群晖只能暂时放下。想到自己还有台裸机安装的黑群晖,尝试后moments正常工作。替换ffmpeg为第三方替换的目的是为了能够正确的生成预览图、视频等。dsm版本及内核uname-a#LinuxvmDSM3.10.105#25426SMPTueMay1204:53:1

    2022年5月6日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号